package ui.activity.profit;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import net.ck.hzq.R;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import base.BaseAct;
import butterknife.BindView;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.OnLoadMoreListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import cn.finalteam.loadingviewfinal.RecyclerViewFinal;
import dao.MerchantTradeItem;
import event.NetErrorEvent;
import ui.activity.profit.biz.MerchantTradeBiz;
import ui.activity.profit.component.DaggerMerchantTradeComponent;
import ui.activity.profit.contract.MerchantTradeContract;
import ui.activity.profit.module.MerchantTradeModule;
import ui.activity.profit.presenter.MerchantTradePresenter;
import ui.adapter.MerchantTradeAdapter;
import util.AlertUtils;
import util.StringUtils;
import util.SysUtils;


/**
 * 交易明细详情
 * Created by chenjian on 2017/8/4.
 */

public class MerchantTradeAct extends BaseAct implements MerchantTradeContract.View {


    @Inject
    MerchantTradePresenter presenter;
    @Inject
    MerchantTradeBiz biz;

    @BindView(R.id.pull_refresh_rv)
    RecyclerViewFinal rv;
    @BindView(R.id.pull_refresh_ptr)
    PtrClassicFrameLayout ptr;
    @BindView(R.id.profit_day)
    TextView day;
    @BindView(R.id.profit_check)
    TextView check;

    MerchantTradeAdapter adp;
    String month;
    String merchantNo;
    String selectMonth;
    long selectTime;

    @Override
    protected void setRoot() {
//        setTitleBar(R.color.colorBlue);
        setContentView(R.layout.activity_merchant_trade);
    }

    @Override
    protected void setup() {
        DaggerMerchantTradeComponent.builder().merchantTradeModule(new MerchantTradeModule(this)).build().inject(this);
        setToolBar(getResources().getString(R.string.lang_trade_details), "");
        presenter.setBiz(biz);
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        selectTime=System.currentTimeMillis();
        month = StringUtils.isEmpty(getIntent().getStringExtra("month")) ? "" : getIntent().getStringExtra("month");
        merchantNo = StringUtils.isEmpty(getIntent().getStringExtra("merchantNo")) ? "" : getIntent().getStringExtra("merchantNo");
        day.setText(month+".01");
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setItemAnimator(new DefaultItemAnimator());
        rv.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        ptr.setLastUpdateTimeRelateObject(this);
        ptr.disableWhenHorizontalMove(true);
        rv.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                presenter.getInfo(false, merchantNo, month);
            }
        });
        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.getInfo(true, merchantNo, month);
            }
        });
        RxView.clicks(day)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> {
                    TimePickerDialog timePickerDialog = AlertUtils.timePicker2(MerchantTradeAct.this, 365 * 5, 0,selectTime, new OnDateSetListener() {
                        @Override
                        public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                            selectTime=millseconds;
                            String str = SysUtils.sf1.format(new Date(millseconds)).replace("-", ".");
                            day.setText(str);
                        }
                    });
                    timePickerDialog.show(MerchantTradeAct.this.getSupportFragmentManager(), "all");
                });

        RxView.clicks(check)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.getDayInfo(true, merchantNo, day.getText().toString().trim()));
        ptr.autoRefresh(true);
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }

    @Override
    public void refreshComplete() {
        ptr.onRefreshComplete();
    }

    @Override
    public void upDateUI(List<MerchantTradeItem> bean) {
        if (adp == null) {
            adp = new MerchantTradeAdapter(MerchantTradeAct.this, bean);
            rv.setAdapter(adp);
        } else {
            adp.notifyDataSetChanged();
        }
    }


    @Override
    public void loadMoreComplete() {
        rv.onLoadMoreComplete();
    }

    @Override
    public void hasLoadMore(boolean b) {
        rv.setHasLoadMore(b);
    }

}
